package com.example.demo.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Service.UserService;
import com.example.demo.entity.User;
import com.example.demo.utils.RedisUtils;
import com.example.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author yangzai
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-goods")
public class UserController {
    private UserService userService;
    private RedisUtils redisUtils;

    private RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

//    @PostMapping("/sdsa")
//    public String list() {
//        return userService.list(new User()) + "";
//    }

    @PostMapping("/ceshi")
    public String chaxun(String id) {
        Object o = redisUtils.get(id);
        Object a = !StringUtils.isEmpty(o) ? JSONObject.parseObject((String) o, User.class) :
                redisUtils.set(id, (JSON.toJSONString(userService.cha(id))));
        rabbitTemplate.convertAndSend("spring.boot.delay.exchange",
                "spring.boot.delay.routingkey", id);
        if(a instanceof Boolean){
            a=redisUtils.get(id);
        }
        return a instanceof User ? "在缓存中找到啦" + o : "在数据库中找到啦"+a;
    }

    @PostMapping("list/{pageNum}/{pageSize}")
    public Result lists(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize
//                        @RequestBody User user
    ){
        PageHelper.startPage((pageNum==null || pageNum==0)?1:pageNum, pageSize==null?5:pageSize);
        PageInfo<User> userPageInfo = new PageInfo<>(userService.list(new User()));
        return Result.ok().data("list",userPageInfo);
    }
    @PostMapping("xiugaihuixian/{gid}")
    public Result xiugaihuixian(@PathVariable("gid") String gid){
        User cha = userService.cha(gid);
        return Result.ok().data("byId",cha);
    }
    @PostMapping("xiugai")
    public Result xiugai(@RequestBody User user){
        System.out.println(user);
        Integer xiugaibiao = userService.xiugaibiao(user);
        return xiugaibiao==0?Result.ok().msg("修改成功"):Result.error().msg("修改失败");
    }

    @PostMapping("piliangshanchu")
    public Result piliangshanchu(
//            @RequestBody Integer[] gids
    ){
        Integer[] gids={3,4,5};
//        gids[0]=1;
//        gids[1]=2;
        boolean b = userService.removeByIds(gids);
        return b?Result.ok().msg("删除成功"):Result.error().msg("删除失败");
    }
}
